var imgNo = "<img width='15px' src='images/n.png'/>";
var imgYes = "<img width='15px' src='images/y.png'>";

function validateTip(element,css,tipString,status){
    element.css(css);
    element.html(tipString);
    element.attr("validateStatus",status);
}
$(function () {

    var nuserName = $("#nuserName");
    var naddress = $("#naddress");
    var npostCode = $("#npostCode");
    var nphoneNumber = $("#nphoneNumber");
    var ADDRESS = $("#ADDRESS");

    var t1 = $("#t1");
    var t2 = $("#t2");
    var t3 = $("#t3");
    var t4 = $("#t4");

    var s1 = $("#s1");
    var s2 = $("#s2");
    var s3 = $("#s3");
    var s4 = $("#s4");

   var addressForm;
   var userId;
   var th;
   view();
   function view() {
       addressForm = $("#addressForm");
       userId = localStorage.getItem("userId");
       th="";
       $.ajax({
           url : "easybuy/address/viewByuserId",
           data : {"userId": userId},
           dataType : "json",
           type : "post",
           success : function (data) {
               th = "<tr><td>备注</td><td>电话号码</td><td>收货地址</td><td>收货人姓名</td><td colspan='3'>操作</td></tr>";
               addressForm.append(th);
               for(var i=0;i<data.length;i++){
                   addressForm.append(
                        "<tr><td>"+data[i].remark+"</td><td>"+data[i].phoneNumber+"</td>"+
                        "<td>"+data[i].address+"</td><td>"+data[i].userName+"</td>"+
                        "<td><a href='' style=\"color:#ff4e00;\" onclick='set("+data[i].id+")' class='default'>"+viewDefault(data[i].isDefault)+"</a>&nbsp; &nbsp;</td>"+
                        "<td><a href='javascript:;' style=\"color:#ff4e00;\" onclick='update("+data[i].id+")'>修改</a>&nbsp; &nbsp;</td>"+
                        "<td><a href='' style=\"color:#ff4e00;\" onclick='del("+data[i].id+")'>删除</a></td></tr>"
                    );
               }
           },
           error : function (XMLHttpRequest, textStatus, errorThrown) {
               //alert("出现错误");
               console.log(XMLHttpRequest.status);
               console.log(XMLHttpRequest.readyState);
               console.log(textStatus);
           }

       });
   }
   function viewDefault(isDefault){
        if(isDefault=="1"){
            return "";
        }else{
            return "设为默认";
        }
   }

   function addCss(){



   }


    nuserName.bind("blur",function () {
        var pattern = /^[\u0391-\uFFE5A-Za-z]+$/;
        if(nuserName.val()=="" || nuserName.val()==null){
            validateTip(t1,{"color": "red"},imgNo + " *姓名不能为空",false);
        }else if(!pattern.test(nuserName.val())){
            validateTip(t1,{"color": "red"}, imgNo +" *输入字符非法",false);
        }else if(nuserName.val().length<2 || nuserName.val().length>20){
            validateTip(t1,{"color": "red"}, imgNo +" *姓名长度错误",false);
        }else {
            validateTip(t1,{"color": "red"}, imgYes,true);
        }
    });

    naddress.bind("blur",function () {
       if(naddress.val()==null || naddress.val()==""){
           validateTip(t2,{"color": "red"},imgNo + " *请填入详细地址",false);
       }else if(naddress.val().length>30){
           validateTip(t2,{"color": "red"},imgNo + " *输入字符数过长",false);
       }else {
           validateTip(t2,{"color": "red"}, imgYes,true);
       }
    });

    npostCode.bind("blur",function () {
       var pattern = /^[0-9]{6}$/;
       if(npostCode.val() == null || npostCode.val()==""){
           validateTip(t3,{"color": "red"},imgNo + " *邮编不能为空",false);
       }else if(!pattern.test(npostCode.val())){
           validateTip(t3,{"color": "red"},imgNo + " *请填有效邮编",false);
       }else {
           validateTip(t3,{"color": "red"}, imgYes,true);
       }
    });


    nphoneNumber.bind("blur",function () {
        var pattern = /^1[34578]\d{9}$/;
        if (nphoneNumber.val() == "" || nphoneNumber.val()==null) {
            validateTip(t4, {"color": "red"}, imgNo+" *手机号码不能为空", false);
        } else if (!pattern.test(nphoneNumber.val())) {
            validateTip(t4, {"color": "red"}, imgNo+" *手机号码格式错误", false);
        } else {
            validateTip(t4, {"color": "red"}, imgYes, true);
        }
    });

    $("#insert").bind("click",function () {
        if(t1.attr("validateStatus") != "true" ){
            nuserName.blur();
        }else if(t2.attr("validateStatus") != "true"){
            naddress.blur();
        }else if(t3.attr("validateStatus") != "true"){
            npostCode.blur();
        }else if(t4.attr("validateStatus") != "true"){
            nphoneNumber.blur();
        }else{
            if(confirm("确认提交")){
                $("#userId").val(localStorage.getItem("userId"));
                var province = $("#TeprovinceName").find("option:selected").text();
                var city = $("#TecityName").find("option:selected").text();
                var district = $("#TedistrictName").find("option:selected").text();
                var position = province+city+district+naddress.val();
                ADDRESS.val(position);
                //console.log(position);
                $.ajax({
                    url : "easybuy/address/insert",
                    type: "post",
                    data: $("#Form").serialize(),
                    dataType: "json",
                    success : function (data) {
                        if(data!=null && data>0){
                            alert("新增成功");
                            location.reload();
                        }
                    },
                    error : function (XMLHttpRequest, textStatus, errorThrown) {
                        //alert("出现错误");
                        console.log(XMLHttpRequest.status);
                        console.log(XMLHttpRequest.readyState);
                        console.log(textStatus);
                    }
                });
            }
        }
    });

    $("#p3").bind("blur",function () {
        var pattern = /^[\u0391-\uFFE5A-Za-z]+$/;
        if($("#p3").val()=="" || $("#p3").val()==null){
            validateTip(s1,{"color": "red"},imgNo + " *姓名不能为空",false);
        }else if(!pattern.test($("#p3").val())){
            validateTip(s1,{"color": "red"}, imgNo +" *输入字符非法",false);
        }else if($("#p3").val().length<2 || $("#p3").val().length>20){
            validateTip(s1,{"color": "red"}, imgNo +" *姓名长度错误",false);
        }else {
            validateTip(s1,{"color": "red"}, imgYes,true);
        }

    });

    $("#p4").bind("blur",function () {
        if($("#p4").val()==null || $("#p4").val()==""){
            validateTip(s2,{"color": "red"},imgNo + " *请填入详细地址",false);
        }else if($("#p4").val().length>30){
            validateTip(s2,{"color": "red"},imgNo + " *输入字符数过长",false);
        }else {
            validateTip(s2,{"color": "red"}, imgYes,true);
        }
    });

    $("#p5").bind("blur",function () {
        var pattern = /^[0-9]{6}$/;
        if($("#p5").val() == null || $("#p5").val()==""){
            validateTip(s3,{"color": "red"},imgNo + " *邮编不能为空",false);
        }else if(!pattern.test($("#p5").val())){
            validateTip(s3,{"color": "red"},imgNo + " *请填有效邮编",false);
        }else {
            validateTip(s3,{"color": "red"}, imgYes,true);
        }
    });

    $("#p6").bind("blur",function () {
        var pattern = /^1[34578]\d{9}$/;
        if ($("#p6").val() == "" || $("#p6").val()==null) {
            validateTip(s4, {"color": "red"}, imgNo+" *手机号码不能为空", false);
        } else if (!pattern.test($("#p6").val())) {
            validateTip(s4, {"color": "red"}, imgNo+" *手机号码格式错误", false);
        } else {
            validateTip(s4, {"color": "red"}, imgYes, true);
        }
    });



    
    $("#update").bind("click",function () {
        var p4 = $("#p4");
        var p2 = $("#p2");
        if(s1.attr("validateStatus") != "true" ){
            $("#p3").blur();
        }else if(s2.attr("validateStatus") != "true"){
            $("#p4").blur();
        }else if(s3.attr("validateStatus") != "true"){
            $("#p5").blur();
        }else if(s4.attr("validateStatus") != "true"){
            $("#p6").blur();
        }else {
            if(confirm("确认修改")){

                var province = $("#eprovinceName").find("option:selected").text();
                var city = $("#ecityName").find("option:selected").text();
                var district = $("#edistrictName").find("option:selected").text();
                var position = province+city+district+p4.val();
                p2.val(position);
                console.log(position);
                $.ajax({
                    url : "easybuy/address/update",
                    data : $("#updateFrom").serialize(),
                    dataType : "json",
                    type : "post",
                    success : function (data) {
                        if(data !=null && data>0){
                            alert("修改成功");
                            location.reload();
                        }
                    },
                    error : function (XMLHttpRequest, textStatus, errorThrown) {
                        console.log(XMLHttpRequest.status);
                        console.log(XMLHttpRequest.readyState);
                        console.log(textStatus);
                    }
                });

            }

        }
    })

});

function del(id) {
    $.ajax({
        url : "easybuy/address/del",
        data : {"id" : id},
        dataType : "json",
        type : "post",
        success : function (data) {
            if(data!=null && data>0){
                alert("删除成功");
                location.reload();
            }
        },
        error : function (XMLHttpRequest, textStatus, errorThrown) {
            //alert("出现错误");
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }

    });
}
function update(id){
    var Id = $("#p1");
    var address = $("#p2");
    var userName = $("#p3");
    var PostCode = $("#p5");
    var PhoneNumber = $("#p6");
    var remark = $("#p7");
    Id.val(id);
    $.ajax({
        url : "easybuy/address/viewById",
        data : {"ID": id},
        dataType : "json",
        type : "post",
        success : function (data) {
            //userId.val(data.userId);
            userName.val(data.userName);
            PostCode.val(data.postCode);
            PhoneNumber.val(data.phoneNumber);
            remark.val(data.remark);

        },
        error : function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }

    });

}

function set(id){
    var userId = localStorage.getItem("userId");
    $.ajax({
        url : "easybuy/address/setdefault",
        dataType : "json",
        data : {"userId":userId,"id":id},
        type : "post",
        success : function (data) {
            if(data>0){
                alert("默认地址设置完成");
                location.reload();
            }
        },
        error : function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }

    });

}

